package com.dd.cloud.user.service.resource.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.Assert;
import com.dd.cloud.common.utils.ValidatorUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.resource.AdvertInfo;
import com.dd.cloud.user.mapper.resource.AdvertInfoMapper;
import com.dd.cloud.user.req.resource.*;
import com.dd.cloud.user.res.resource.AdvertInfoSystemDetailRes;
import com.dd.cloud.user.res.resource.AdvertInfoSystemPageRes;
import com.dd.cloud.user.service.resource.IAdvertInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/29
 */
@Slf4j
@Service
public class AdvertInfoServiceImpl extends ServiceImpl<AdvertInfoMapper, AdvertInfo> implements IAdvertInfoService {
    /**
     * 查询广告弹框列表
     *
     * @param req
     * @return
     */
    @Override
    public IPage<AdvertInfoSystemPageRes> queryAdvertInfoSystemPage(AdvertInfoSystemPageReq req) {
        Assert.verifyStatusIdsParam(req.getClass(), req);
        if (StringUtils.isBlank(req.getStatus())) {
            req.setStatus("1,2,3,4");
        }
        IPage<AdvertInfoSystemPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryAdvertInfoSystemPage(iPage, req);
        return iPage;
    }

    /**
     * 创建广告弹框
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAdvertInfoSystem(AdvertInfoSystemCreateReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        if (req.getStatus() == null) {
            ValidatorUtils.verify(req, req.getClass());
        } else {
            if (StringUtils.isBlank(req.getTitle())) {
                throw new ZekeException(400, "广告名称不能为空");
            }
        }
        AdvertInfo advertInfo = new AdvertInfo();
        BeanUtils.copyProperties(req, advertInfo);
        if (req.getStatus() == null) {
            advertInfo.setStatus(1);
//            LocalDateTime now = LocalDateTime.now();
//            if (req.getStart().isBefore(now)) {
//                if (req.getEnd().isAfter(now)) {
//                    advertInfo.setStatus(1);
//                } else {
//                    advertInfo.setStatus(3);
//                }
//            } else {
//                advertInfo.setStatus(2);
//            }
        }
        advertInfo.setLastId(loginInfo.getId());
        advertInfo.setLastType(loginInfo.getType());
        advertInfo.setCreateDate(LocalDateTime.now());
        advertInfo.setUpdateDate(LocalDateTime.now());
        boolean flag = save(advertInfo);
        if (!flag) {
            throw new ZekeException(ZkExceptionEnum.ADD_ERROR);
        }
    }

    /**
     * 修改广告弹框
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdvertInfoSystemUpdate(AdvertInfoSystemUpdateReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        if (req.getStatus() == null) {
            ValidatorUtils.verify(req, req.getClass());
        } else {
            if (req.getId() == null) {
                throw new ZekeException(400, "广告id不能为空");
            }
            if (StringUtils.isBlank(req.getTitle())) {
                throw new ZekeException(400, "广告名称不能为空");
            }
        }
        AdvertInfo advertInfo = new AdvertInfo();
        BeanUtils.copyProperties(req, advertInfo);
        if (req.getStatus() == null) {
            advertInfo.setStatus(1);
//            LocalDateTime now = LocalDateTime.now();
//            if (req.getStart().isBefore(now)) {
//                if (req.getEnd().isAfter(now)) {
//                    advertInfo.setStatus(1);
//                } else {
//                    advertInfo.setStatus(3);
//                }
//            } else {
//                advertInfo.setStatus(2);
//            }
        }
        advertInfo.setLastId(loginInfo.getId());
        advertInfo.setLastType(loginInfo.getType());
        advertInfo.setUpdateDate(LocalDateTime.now());
        boolean flag = updateById(advertInfo);
        if (!flag) {
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }

    /**
     * 修改广告弹框状态
     *
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdvertInfoSystemUpdateStatus(AdvertInfoSystemUpdateStatusReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        if (req.getStatus() == 1) {
            if (req.getStart() == null || req.getEnd() == null) {
                throw new ZekeException(400, "截止日期不能为空");
            }
        }
        AdvertInfo advertInfo = new AdvertInfo();
        advertInfo.setId(req.getId());
        advertInfo.setStatus(req.getStatus());
        advertInfo.setStart(req.getStart());
        advertInfo.setEnd(req.getEnd());
        advertInfo.setLastId(loginInfo.getId());
        advertInfo.setLastType(loginInfo.getType());
        advertInfo.setUpdateDate(LocalDateTime.now());
        boolean flag = updateById(advertInfo);
        if (!flag) {
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }

    /**
     * 查询广告弹框详情
     *
     * @param id
     * @return
     */
    @Override
    public AdvertInfoSystemDetailRes queryAdvertInfoSystemDetail(Integer id) {
        AdvertInfoSystemDetailRes res = getBaseMapper().queryAdvertInfoSystemDetail(id);
        if (res == null) {
            return new AdvertInfoSystemDetailRes();
        }
        res.setStatisticRes(getBaseMapper().queryAdvertInfoStatisticData(id));
        return res;
    }

    @Override
    public Page<AdvertInfo> queryDoctorAdvertPage(AdvertInfoReq req) {
        LambdaQueryWrapper<AdvertInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (req.getPictureType() != null) {
            queryWrapper.eq(AdvertInfo::getPictureType, req.getPictureType());
        }
        if (req.getPosition() != null) {
            queryWrapper.eq(AdvertInfo::getPosition, req.getPosition());
        }
        if (req.getCrowd() != null) {
            queryWrapper.eq(AdvertInfo::getCrowd, req.getCrowd());
        }
        queryWrapper.eq(AdvertInfo::getStatus, 1);
        Page<AdvertInfo> page = req.pagePojo();
        page = this.page(page, queryWrapper);
        return page;
    }

    /**
     * 定时任务修改广告状态
     */
    @Override
    public void taskUpdateStatus() {
        UpdateWrapper<AdvertInfo> uw = new UpdateWrapper<>();
        uw.lambda().eq(AdvertInfo::getStatus, 1);
        uw.lambda().le(AdvertInfo::getEnd, LocalDateTime.now());
        uw.lambda().set(AdvertInfo::getStatus, 3);
        update(uw);
    }
}
